<?php


	namespace app\controller\shop;


	use app\BaseController;
	use app\Request;
	use app\services\AliOssServices;
	use think\facade\Filesystem;

	class CommonController extends BaseController
	{
		public function upload (Request $request, $extType) {
			$file = $this->request->file('file');
			if (empty($file)) {
				return toast('', '上传的文件为空');
			}

			$ext = $file->getOriginalExtension();

			//格式校验
			$extArray = [];
			if ($extType == 'image') {
				$extArray = ['jpg', 'png', "jpeg", "gif", "bmp"];
			} else if ($extType == 'video') {
				$extArray = ['mp4', 'avi', 'wmv', 'rm'];
			}

			if (!in_array($ext, $extArray)) {
				return toast('请上传正确的格式');
			}

			$open = false;

			if ($open) {
				$filePath = $file->getRealPath();
				$aliOssServices = new AliOssServices();
				$ret = $aliOssServices->upload($filePath, $ext);

				if ($ret['info']['url']) {
					return success($ret['info']['url']);
				}

				return toast($ret);
			} else {
				$ret = Filesystem::disk('public')->putFile('image', $file);
				$ret = str_replace('\\', '/', $ret);
				$ret = 'storage/' . $ret;

				return success($ret);
			}

		}
	}





